//
//  THNetCheck.m
//  THBaseApp
//
//  Created by TryHone on 16/6/3.
//  Copyright © 2016年 TryHone. All rights reserved.
//

#import "JFNetCheck.h"
#import "JFLogHelper.h"
#import "JFBase.h"

#if __has_include(<AFNetworking/AFNetworking.h>)
#import <AFNetworking/AFNetworking.h>
#else
#import "AFNetworking.h"
#endif

@implementation JFNetCheck{
    AFNetworkReachabilityManager* _netManager;
}

+ (JFNetCheck *)sharedManager
{
    static JFNetCheck *sharedTHNetCheckManagerInstance = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        sharedTHNetCheckManagerInstance = [[self alloc] init];
    });
    return sharedTHNetCheckManagerInstance;
}

-(instancetype)init{

    self=[super init];
    
    if (self) {
        _netManager = [AFNetworkReachabilityManager sharedManager];
    }
    return self;
}
-(NetStatus)status{

    switch (_netManager.networkReachabilityStatus) {
        case AFNetworkReachabilityStatusUnknown:
            return NetStatus_Unknown;
            break;
                    
        case AFNetworkReachabilityStatusNotReachable:
            return NetStatus_NotReachable;
            break;
        case AFNetworkReachabilityStatusReachableViaWWAN:
            return NetStatus_ReachableViaWWAN;
            break;
                    
        case AFNetworkReachabilityStatusReachableViaWiFi:
            return NetStatus_ReachableViaWiFi;
            break;
        default:
            return NetStatus_Unknown;
            break;
}

}
-(void)netStatus:(JFNetCheckStatusBlock)statusblock{
    
        [JFNetCheck sharedManager];
        [_netManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
            
            NetStatus state;
            switch (status) {
                case AFNetworkReachabilityStatusUnknown:
                    [JFLogHelper stringWriteLogToConsole:@"未识别的网络"];
                    state = NetStatus_Unknown;
                    statusblock(NetStatus_Unknown);
                    break;
                    
                case AFNetworkReachabilityStatusNotReachable:
                     [JFLogHelper stringWriteLogToConsole:@"不可达的网络(未连接)"];
                    state = NetStatus_NotReachable;
                    statusblock(NetStatus_NotReachable);
                    break;
                case AFNetworkReachabilityStatusReachableViaWWAN:
                    [JFLogHelper stringWriteLogToConsole:@"2G,3G,4G...的网络"];
                    state = NetStatus_ReachableViaWWAN;
                    statusblock(NetStatus_ReachableViaWWAN);
                    break;
                    
                case AFNetworkReachabilityStatusReachableViaWiFi:
                    [JFLogHelper stringWriteLogToConsole:@"wifi的网络"];
                    state = NetStatus_ReachableViaWiFi;
                    statusblock(NetStatus_ReachableViaWiFi);
                    break;
                default:
                    break;
            }
            
            AWDISPATCH_MAIN_THREAD(^{
                [[NSNotificationCenter defaultCenter] postNotificationName:AW_NETWORKCHANGE object:@(status) userInfo:nil];
            })
            
        }];
        
        [_netManager startMonitoring];//开始监听网络状态
}

+(void)netStatus:(JFNetCheckStatusBlock)statusblock{
    [[JFNetCheck sharedManager] netStatus:^(NetStatus status) {
        statusblock(status);
    }];
}

-(void)dealloc{
    [_netManager stopMonitoring];//停止监听
    _netManager = nil;
}
@end
